home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / buzzmachines_massive.exe / Dev / Geoffroy Notefilter SourceCode / NoteFilterTrack.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-05-25  |  5.4 KB  |  205 lines

  1. // NoteFilterTrack.cpp: implementation of the NoteFilterTrack class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4.  
  5. #include "NoteFilterTrack.h"
  6. #include "../dsplib.h"
  7. #include "BuzzParameterFilterType.h"
  8.  
  9. //////////////////////////////////////////////////////////////////////
  10. // Construction/Destruction
  11. //////////////////////////////////////////////////////////////////////
  12.  
  13. NoteFilterTrack::NoteFilterTrack()
  14. {
  15.     oldValueForQ = 0;
  16.     oldValueForA = oldValueForD = oldValueForS = oldValueForR = 0;
  17.     a = d = s = r = NULL;
  18.     q = NULL;
  19.     volume = NULL;
  20.     adsrActivated = false;
  21.  
  22.     for (int i = 0; i<NoteFilterTrack_NB_HARMONICS; i++) {
  23.         bandPassFilter[i].setType(CookbookFilter.BPF);
  24.         bandPassFilter[i].setGain(0.0f);
  25.         oldValueForHarmoVolumes[i] = 0.0f;
  26.     }
  27.  
  28.     trackBufferTempSize = 4096;
  29.     trackBufferTemp = (float *)malloc(trackBufferTempSize*sizeof(float));
  30.  
  31.     mixBufferTempSize = 4096;
  32.     mixBufferTemp = (float *)malloc(mixBufferTempSize*sizeof(float));
  33. }
  34.  
  35. NoteFilterTrack::~NoteFilterTrack()
  36. {
  37.     free(trackBufferTemp);
  38.     free(mixBufferTemp);
  39. }
  40.  
  41. void NoteFilterTrack::setNote(byte note) 
  42. {
  43.     // change the frequency
  44.     float fundamental = buzzNote2Freq(note);
  45.  
  46.     for (int i = 0; i<NoteFilterTrack_NB_HARMONICS; i++) {
  47.         bandPassFilter[i].setFrequency(fundamental*(i+1));
  48.     }
  49.  
  50.     // reset ADSR
  51.     adsrEnveloppe.reset();
  52. }
  53.  
  54. void NoteFilterTrack::setQ(BuzzParameterQ * q) 
  55. {
  56.     this->q = q;
  57. }
  58.  
  59. bool NoteFilterTrack::MDKWorkStereo(float *psamples, int numsamples, int const mode) 
  60. {
  61.     /*
  62.      * update inertia for track parameters 
  63.      */
  64.  
  65.     adsrEnveloppe.timeGoesBy(numsamples);
  66.  
  67.     // wonder if Q has changed since last time
  68.     if (q != NULL) if (q->getRealValue() != oldValueForQ) {
  69.         oldValueForQ = q->getRealValue();
  70.         for (int i = 0; i<NoteFilterTrack_NB_HARMONICS; i++) {
  71.             bandPassFilter[i].setQ(oldValueForQ);
  72.         }
  73.     }
  74.  
  75.     // gain
  76.     
  77.     // wonder if a,d,s or r has changed since last time
  78.     if (a!=NULL && d!=NULL && s!=NULL && r!=NULL && 
  79.         (a->getRealValue() != oldValueForA ||
  80.          d->getRealValue() != oldValueForD ||
  81.          s->getRealValue() != oldValueForS ||
  82.          r->getRealValue() != oldValueForR)) {
  83.         // change ADSR
  84.         oldValueForA = a->getRealValue();
  85.         oldValueForD = d->getRealValue();
  86.         oldValueForS = s->getRealValue();
  87.         oldValueForR = r->getRealValue();
  88.  
  89.         adsrEnveloppe.setA(oldValueForA);
  90.         adsrEnveloppe.setD(oldValueForD);
  91.         adsrEnveloppe.setS(oldValueForS);
  92.         adsrEnveloppe.setR(oldValueForR);
  93.     }
  94.  
  95.     // get the gain from the ADSR enveloppe
  96.     float adsrGain = volume->getRealValue();
  97.     if (adsrActivated) {
  98.         adsrGain *= adsrEnveloppe.getGain();
  99.     }
  100.  
  101.     // wonder if the harmonics volume has changed since last time too
  102.     BuzzParameterVolume * ptBuzzParameterVolume;
  103.     for (int j = 0; j < NoteFilterTrack_NB_HARMONICS; j++) {
  104.         ptBuzzParameterVolume = &(harmoVolumes[j]);
  105.  
  106.         // volume of harmonics has changed
  107.         if (ptBuzzParameterVolume != NULL) if (ptBuzzParameterVolume->getRealValue() != oldValueForHarmoVolumes[j]) {
  108.             oldValueForHarmoVolumes[j] = ptBuzzParameterVolume->getRealValue();
  109.         }
  110.         
  111.         bandPassFilter[j].setGain(oldValueForHarmoVolumes[j] * adsrGain);
  112.     }
  113.  
  114.     // see if the temporary buffer is large enough
  115.     if (numsamples * 2 > trackBufferTempSize) {
  116.         realloc(trackBufferTemp,numsamples * 2 * sizeof(float));
  117.         realloc(mixBufferTemp,numsamples * 2 * sizeof(float));
  118.         trackBufferTempSize = numsamples;
  119.         mixBufferTempSize = numsamples;
  120.     }
  121.  
  122.     // zero the mixBuffer
  123.     DSP_Zero(mixBufferTemp,numsamples*2);
  124.  
  125.     // go through all the harmonics
  126.     for (int i = 0; i<NoteFilterTrack_NB_HARMONICS; i++) {
  127.         // copy the samples in the temporary buffer
  128.         DSP_Copy(trackBufferTemp, psamples, numsamples*2);
  129.  
  130.         // filter the thing
  131.         if (bandPassFilter[i].MDKWorkStereo(trackBufferTemp,numsamples,mode))
  132.         {
  133.             // mix it with the output
  134.             DSP_Add(mixBufferTemp,trackBufferTemp,numsamples*2);
  135.         }
  136.     }
  137.  
  138.     // copy the output to psamples
  139.     DSP_Copy(psamples, mixBufferTemp, numsamples*2);
  140.     
  141.     return true;
  142. }
  143.  
  144. void NoteFilterTrack::setSampleRate(int sampleRate)
  145. {
  146.     for (int i = 0; i<NoteFilterTrack_NB_HARMONICS; i++) {
  147.         bandPassFilter[i].setSampleRate(sampleRate);
  148.     }
  149. }
  150.  
  151. float NoteFilterTrack::buzzNote2Freq(byte buzzNote)
  152. {
  153.     static float base_freq = 16.3516f;
  154.  
  155.     int Note = (buzzNote>>4)*12+(buzzNote&0x0f) -1;
  156.     return (float)(base_freq* pow(2.0, Note/12.0));
  157. }
  158.  
  159. void NoteFilterTrack::setHarmoVolumes(BuzzParameterVolume * harmoVolumes)
  160. {
  161.     this->harmoVolumes = harmoVolumes;
  162. }
  163.  
  164. void NoteFilterTrack::setFilterType(byte filterType)
  165. {
  166.     CookbookFilter::filtertype cookbookType;
  167.  
  168.     switch (filterType) {
  169.     case BuzzParameterFilterType_BPF:
  170.         cookbookType = CookbookFilter::BPF;
  171.         break;
  172.     case BuzzParameterFilterType_NOTCH:
  173.         cookbookType = CookbookFilter::NOTCH;
  174.         break;
  175.     case BuzzParameterFilterType_PEAK:
  176.         cookbookType = CookbookFilter::PEAKINGEQ;
  177.         break;
  178.     default:
  179.         cookbookType = CookbookFilter::LPF;
  180.         break;
  181.     }
  182.  
  183.     for (int i = 0; i<NoteFilterTrack_NB_HARMONICS; i++) {
  184.         bandPassFilter[i].setType(cookbookType);
  185.     }
  186. }
  187.  
  188. void NoteFilterTrack::setADSR(BuzzParameterDuration *a, BuzzParameterDuration *d, BuzzParameterDuration *s, BuzzParameterDuration *r)
  189. {
  190.     this->a = a;
  191.     this->d = d;
  192.     this->s = s;
  193.     this->r = r;
  194. }
  195.  
  196. void NoteFilterTrack::activateADSR(bool activate)
  197. {
  198.     this->adsrActivated = activate;
  199. }
  200.  
  201. void NoteFilterTrack::setVolume(BuzzParameterVolume *trackVolume)
  202. {
  203.     this->volume = trackVolume;
  204. }
  205.